package com.daxue.commons.apache.lang3;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author daxue0929
 * @date 2022/08/26
 **/

public class ArrayUtilsTest {


    @Test
    public void test005() {



    }

    @Test
    public void test004() {
        // 1.初始化数组
        int[] firstArray = new int[3];
        firstArray[0] = 0;
        firstArray[1] = 1;
        firstArray[2] = 2;

        System.out.println(Array.getLength(firstArray));

        //简单类型的数组转为包装类型的数组，转为ArrayList
        Integer[] integers = ArrayUtils.toObject(firstArray);
        ArrayList<Integer> list = new ArrayList<>(integers.length);
        CollectionUtils.addAll(list, integers);
        System.out.println("123");

        // 包装类数组转为 List
        Integer[] array = {4,5,6};
        List<Integer> resultList= new ArrayList<>(Arrays.asList(array));

        // List转为数组
        Integer[] array1 = resultList.toArray(new Integer[resultList.size()]);

        // 包装类数组转基本类数组
        int[] ints = ArrayUtils.toPrimitive(array1);

        System.out.println("stop here and look stack");
    }



    @Test
    public void test003() {
        // 1.初始化数组
        int[] firstArray = new int[3];
        firstArray[0] = 0;
        firstArray[1] = 1;
        firstArray[2] = 2;

        boolean contains = ArrayUtils.contains(firstArray, 2);
        if (contains) {
            System.out.println("包含指定元素");
        }
    }



    @Test
    public void test001() {
        // 1.初始化数组
        int[] firstArray = new int[3];
        firstArray[0] = 0;
        firstArray[1] = 1;
        firstArray[2] = 2;

        // 向数组中追加一个值，返回的是新的数组
        int[] result = ArrayUtils.add(firstArray, 4);

        int[] secondArray = new int[3];
        secondArray[0] = 3;
        secondArray[1] = 4;
        secondArray[2] = 5;

        // 向数组中追加多个值，返回的是新的数组,非原来的值
        int[] thirdArray = ArrayUtils.addAll(firstArray, secondArray);

        Class<?> componentType = firstArray.getClass().getComponentType();
        Object instance = Array.newInstance(componentType, 3);
        //神奇,打印出来的值很神奇
        System.out.println("Array.newInstance Type: " + instance.getClass());
    }

    @Test
    public void test002() {
        // 1.初始化数组
        int[] firstArray = new int[3];
        firstArray[0] = 0;
        firstArray[1] = 1;
        firstArray[2] = 2;

        int[] cloneArray = firstArray.clone();
        System.out.println("当前对象的hashcode是: " + cloneArray.hashCode());

        int[][] originArray = new int[3][2];
        originArray[0] = new int[]{5, 6};
        originArray[1] = new int[]{7, 8};
        originArray[2] = new int[]{9, 10};

        Class<? extends int[]> aClass = originArray[0].getClass();
        // Object[]
        if (originArray[0] instanceof int[]) {
            System.out.println("进来了");
        }

        // 报错
//        Map<Object, Object> resultMap = ArrayUtils.toMap(originArray);
//        resultMap.entrySet().stream().forEach(
//                item -> System.out.println(item.getKey() + ":" + item.getValue())
//        );
        System.out.println("==========================================================");

    }


    public static void main(String[] args){
    }

}
